package components
{
	import SEApp.se_eventManager;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import classes.clt_events;
	import classes.clt_loader;
	
	import data.cd_utils;
	
	import dialogs.pi_abox;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class clt_box extends pi_abox {
		private var _dataModule: Object;
		protected var _filterList: Array;
		
		public function clt_box(parent:uif_component, a_dataModule: Object, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, atabwidth:int=0, styleClass:String='', countInfo:Boolean=true) {
			_dataModule = a_dataModule;
			super(parent, _dataModule.groups, ax, ay, awidth, aheight, atabwidth, styleClass, countInfo);
			defaultSelectSet();
		}
		
		public function get clt(): Object {
			return app;
		}
		
		public function get sexUser(): int {
			return (clt.user)?clt.user.templateSex:0;
		}
		
		protected function defaultSelectSet(): void {
			function updateDefault(e: Event = null): void {
				if (e) clt.workspace.removeEventListener(clt_events.USERUPDATE, updateDefault);
				
				_dataModule.groups.forEach(function(item:*, index:int, array:Array): void {
					if (item.id == _dataModule.Default[sexUser].group) {
						tabs.selectIndex = index;
						if (_dataModule.Default[sexUser].hasOwnProperty('id')) {
							for (var i:int; i<_itemsLayer.items.length; i++) {
								if (_itemsLayer.items[i].object && 
									(_itemsLayer.items[i].object.id == _dataModule.Default[sexUser].id)) {
									_itemsLayer.items[i].control.select();
									return;
								}
							}
						}
					}
				});				
			}
			if (_dataModule.hasOwnProperty('Default')) {
				if (clt.user.hasOwnProperty('sex')) updateDefault();
				else clt.workspace.addEventListener(clt_events.USERUPDATE, updateDefault);
			}
		}
		
		public function get workspace(): se_eventManager {
			return (app as Object).workspace;
		}
		
		override protected function listenEvents(): void {
			addEvent(Event.SELECT, doItemSelect);
		}
		
		protected function doItemSelect(e: ObjectEvent): void {
			e.stopPropagation();	
		}
		
		public function assignContent(createItemFunc: Function): void {
			var item_width: int = _itemsLayer.calcFrameWidth(rows);
			while (_filterList.length < (Math.floor(_itemsLayer.displayRect.width / item_width) * rows)) _filterList.push(null);
			_itemsLayer.assignList(_filterList, item_width, createItemFunc, rows);
		}
		
		override protected function doChangeSelect(tabIndex: int): void {
			if (tabs.selectItem.object.id < 1000) {
				if (tabs.selectItem.object.url) {
					function doCompleteGroup(e: Event): void {
						e.target.removeEventListener(Event.COMPLETE, doCompleteGroup);
						var list: Array = e.target.loader.content.list;
						cd_utils.resetData(list, tabs.selectItem.object.id);
						_dataModule.list = (_dataModule.list as Array).concat(list);
						delete(tabs.selectItem.object.url);
						doChangeSelect(tabIndex);
					}
					new clt_loader(this, tabs.selectItem.object.url, doCompleteGroup);
				} else {
					_filterList = _dataModule.list.filter(function(item: Object, index: int, arra: Array): Boolean {
						return item.group == tabs.selectItem.object.id;
					}); 
					assignContent(createListItem);
				}
			} else {
				clt.internalServer.oneQuery('getGroupTmpls', tabs.selectItem.object.id, function(a_data: Object): void {
					var g_list: Array = a_data.response[0] as Array;
					g_list = g_list?g_list:[];
					_filterList = _dataModule.list.filter(function(item: Object, index: int, arra: Array): Boolean {
						return g_list.some(function(g_item: Object, index: int, arr: Array): Boolean {
							return g_item.tmpl_id == item.id;
						});
					}); 
					assignContent(createListItem);					
				});
			}
		}
		
		protected function get rows(): int {
			return 2;
		}
		
		protected function createListItem(_scroll: uif_component, inner: Rectangle, index: int): clt_previewItem {
			return null;
		}
	}
}